// index.js
// 获取应用实例
const util = require('../../utils/util.js')
var QQMapWX = require('../../libs/qqmap-wx-jssdk');
var qqmapsdk;
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cityName:"",
    future:{},//今天的天气
    week: {},//存放未来的7个星期
    today:{},//当天的天气
     weatherIcon:"",
     math:[
      {Appid:48543781,appsecret:"5RwfzSeK"},
      {Appid:28231255,appsecret:"jBogk20G"},
      {Appid:58356284,appsecret:"KLj9W5eS"},
      {Appid:26719413,appsecret:"iBM60HE2"},
    ]

     
  },
 
 getCityId:function(event){
  
    this.setData({
      inputCity:event.detail.value
    })
    // this.getResults()
 },
 bindSearch: function () {
  var that = this;
  var city = this.data.inputCity;

  if (city == '') {
    wx.showModal({
      title: '提示',
      content: '请先输入要查询的城市名称',
      confirmText: '好的',
      confirmColor: '#ACB4E3',
      showCancel: false,
    });
  } else {
    let weatherparam = {
      location: city,
      key: 'f10d5759164d412f9dbcfc2312ea1374',
    };

    wx.request({
      url: 'https://geoapi.qweather.com/v2/city/lookup?',
      data: weatherparam,
      header: {
        "content-type": "application/json"
      },
      method: 'GET',

      success(data) {
        if(data.data.code == '404'){
          wx.showModal({
            title: '提示',
            content: '输入的城市名称有误，请重新输入',
            confirmText: '好的',
            confirmColor: '#ACB4E3',
            showCancel: false,
          });
        }
        else{
          city = data.data.location[0].name;
            that.setData({
              cityName:city
            })
            that.getResults()
            that.setData({
              inputCity: '',
            });
        }
      
      },
      fail(data) {
      }
    });

    // 一键回到顶部
   
  }
},

getWeatherIcon(weather){
  
  switch(weather){
      case "晴":
         return "/pics/100.png";
      case "多云":
        return "/pics/103.png";
      case "阴":
        return "/pics/104.png";
      case "阵雨":
        return "/pics/318.png";
      case "雷阵雨转阴":
        return "/pics/302.png";
      case "雨夹雪":
        return "/pics/406.png"; 
      case "小雨":
        return "/pics/309.png"; 
      case "中雨":
        return "/pics/305.png";
      case "大雨":
        return "/pics/306.png";
      case "暴雨":
        return "/pics/307.png"; 
      case "阵雪":
        return "/pics/410.png"; 
      case "小雪":
        return "/pics/407.png";
      case "中雪":
        return "/pics/408.png";
      case "大雪":
        return "/pics/409.png"; 
      case "暴雪":
        return "/pics/410.png";                          
  }
  if (weather.indexOf("雷阵雨")==0){
    return "/pics/302.png";
  }else if (weather.indexOf("阴")==0){
    return "/pics/104.png";
  }else if (weather.indexOf("晴")==0){
    return "/pics/100.png";
  }else if (weather.indexOf("雪")==0){
    return "/pics/410.png";
  }else if (weather.indexOf("多云")==0){
    return "/pics/103.png";
  }else if (weather.indexOf("小雨")==0){
    return "/pics/309.png";
  }else if (weather.indexOf("中雨")==0){
    return "/pics/305.png";
  }else if (weather.indexOf("阵雨")==0){
    return "/pics/318.png";
  }
  
},
 getResults:function(){
   let that=this;
   var index = Math.floor((Math.random()*this.data.math.length)); 
   let appId=this.data.math[index].Appid;
   let appSecret=this.data.math[index].appsecret;
   wx.request({
     url: 'https://www.tianqiapi.com/free/day?appid='+appId+'&appsecret='+appSecret+' ',
     data:{
      city:that.data.cityName
  },
     success:function(rep){
      that.setData({
        future:rep.data
        
      })
     }
   })
   wx.request({
    url: 'https://www.tianqiapi.com/free/week?appid='+appId+'&appsecret='+appSecret+'  ',
    data:{
      city:that.data.cityName
    },
    success:function(rsp){
          //获得每一天的预报
          var daysForecast = [];
          for (var i = 0; i < rsp.data.data.length; i++) {
            daysForecast.push(that.getWeatherIcon(rsp.data.data[i].wea));
          }
  
        let time = util.formatTime(new Date());
         let date=util.getDates(7, time);
         
        that.setData({
          week:date,
          today:rsp.data.data,
          // backImage:that.getBackImg(rsp.data.data[0].wea),
          weatherIcon:that.getWeatherIcon(rsp.data.data[0].wea),
          daysForecast:daysForecast
       })
     
      
    },
    fail:function(err){      
    }
  })
 },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.onNetworkStatusChange(function(res){
      if(res.networkType == "none"){
        wx.showLoading({
          title: '无网络....',
        })
      }else{
        wx.showLoading({
          title: '连接中.....',
        })
        wx.hideLoading();
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        this.getResults();
      }
    })
    this.getResults();
    qqmapsdk = new QQMapWX({
      key: 'V3LBZ-G7IEX-MV54S-ZPJLY-2PIJT-FYFUW'
  });
    
   
  },
  getData(){
    this.onShow();
    this.getResults(this.data.cityName);
     //隐藏loading 提示框
     wx.hideLoading();
     //隐藏导航条加载动画
     wx.hideNavigationBarLoading();
     //停止下拉刷新
     wx.stopPullDownRefresh();
  },
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    setTimeout(() => {
      wx.showLoading({
        title: '刷新中...',
      })
      this.getData();
    }, 1500);
   
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that=this
        qqmapsdk.search({
            keyword: '学院',
            success: function (res) {
                that.setData({
                  cityName:res.data[0].ad_info.district
                }
                
                )
            },
            fail: function (res) {
            },
        
     });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const { info, code } = that.data;
    return {
      title: info.name,
      path: `/pages/share/index?code=${code}&uid=${wx.getStorageSync('uid')}`,
      imageUrl: info.thumbnail_url
    }
  },
  onShareTimeline: function(){
    const { info, code } = that.data;
    return {
      title: info.name,
      query: `code=${code}&uid=${wx.getStorageSync('uid')}`,
      imageUrl: info.thumbnail_url
    }
  }
})